package com.person.jc.web.base.verify;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.person.jc.service.base.verify.BdVerifyBasisService;
import com.person.jc.utils.Servlets;
import com.person.jc.utils.Utils;

public class BdVerifyBasisController extends Controller {
	
	public void init(){
		setAttr("menuId", getPara("menuId"));
		render("main.jsp");
	}
	
	public void list(){
		Map<String,Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		List<Record> list = BdVerifyBasisService.me.getList(map);
		setAttr("list", list);
		render("list.jsp");
	}
	
	public void edit(){
		String id = getPara("id");
		String view = getPara("view");
		if(Utils.isNotEmpty(id)){
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("id", id);
			List<Record> list = BdVerifyBasisService.me.getList(map);
			if(list.size() != 0){
				setAttr("obj", list.get(0));
			}
			List<Record> rel = BdVerifyBasisService.me.getRel(id);
			setAttr("details", rel);
		}
		setAttr("view", view);
		render("edit.jsp");
	}
	
	@Before(Tx.class)
	public void save(){
		String res = "success";
		Map<String,Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		String[] standard1 = getParaValues("standard1");
		String[] standard2 = getParaValues("standard2");
		String id = String.valueOf(map.get("id"));
		if(Utils.isNotEmpty(id)){
			BdVerifyBasisService.me.update(map);
		}else{
			id = BdVerifyBasisService.me.save(map);
		}
		BdVerifyBasisService.me.deleteRel(id);
		if(standard1!=null && standard1.length != 0){
			BdVerifyBasisService.me.saveRel(id, standard1, "1");
		}
		if(standard2!=null && standard2.length != 0){
			BdVerifyBasisService.me.saveRel(id, standard2, "2");
		}
		renderText(res);
	}
	
	public void delete(){
		String res = "success";
		String ids = getPara("ids");
		for (String id : ids.split(",")) {
			BdVerifyBasisService.me.delete(id);
		}
		renderText(res);
	}
	
	public void changeStatus(){
		String res = "success";
		String ids = getPara("ids");
		for(String id :ids.split(",")){
			BdVerifyBasisService.me.changeStatus(id);
		}
		renderText(res);
	}
	
	public void getStandList(){
		String standard1s = getPara("standard1s");
		String standard2s = getPara("standard2s");
		String deviceType = getPara("deviceType");
		String deviceId = getPara("deviceId");
		if(standard1s.endsWith(",")){
			standard1s = standard1s.substring(0, standard1s.length()-1);
		}
		if(standard2s.endsWith(",")){
			standard2s = standard2s.substring(0, standard2s.length()-1);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("standard1s", standard1s);
		map.put("standard2s", standard2s);
		map.put("deviceType", deviceType);
		map.put("deviceId", deviceId);
		List<Record> list = BdVerifyBasisService.me.getStandList(map);
		setAttr("list", list);
		render("standard.jsp");
	}
	
}
